package com.zmx.base.web;

import com.zmx.base.dto.BaseCommunityDto;
import com.zmx.base.dto.BaseMemberCommunityRelationDto;
import com.zmx.base.service.BaseCommunityService;
import com.zmx.base.service.BaseMemberCommunityRelationService;
import com.zmx.common.common.web.AbstractController;
import com.zmx.common.common.web.rest.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 区域数据控制器
 * @ClassName: BaseCommunityController
 * @Author zhaomxr
 * @Date 2022-01-11 15:15
 */
@Api("区域数据")
@Slf4j
@RestController
@RequestMapping("community")
public class BaseCommunityController extends AbstractController<BaseCommunityDto, BaseCommunityService> {

    @Autowired
    private BaseCommunityService baseCommunityService;
    @Autowired
    private BaseMemberCommunityRelationService baseMemberCommunityRelationService;

    @ApiOperation(value = "分页获取所有可选区域", tags = "分页获取所有可选区域")
    @GetMapping("getCommunityByPage")
    public Object getCommunityByPage(@RequestParam(required = true) Double latitude,
                                     @RequestParam(required = true) Double longitude,
                                     @RequestParam(required = true) Integer pageIndex,
                                     @RequestParam(required = true) Integer pageSize) {
        try {
            Page page = baseCommunityService.getCommunityByPage(latitude, longitude, pageIndex, pageSize);
            return ResponseResult.success(page);
        } catch (Exception e) {
            log.error("分页获取所有可选区域失败！", e);
            return ResponseResult.fail("分页获取所有可选区域失败");
        }
    }

    @ApiOperation(value = "查询用户选择的区域", tags = "查询用户选择的区域")
    @GetMapping("getCommunity")
    public Object getCommunity() {
        try {
            BaseMemberCommunityRelationDto memberCommunityRelationDto = baseMemberCommunityRelationService.getCommunity();
            return ResponseResult.success(memberCommunityRelationDto);
        } catch (Exception e) {
            log.error("查询用户选择的区域失败！", e);
            return ResponseResult.fail("查询用户选择的区域失败");
        }
    }

    @ApiOperation(value = "新增区域选择记录", tags = "新增区域选择记录")
    @PostMapping("addMemberCommunity")
    public Object addMemberCommunity(@RequestParam(required = true) Long communityId) {
        log.info("begin to insertMemberCommunity communityId:", communityId);
        try {
            baseMemberCommunityRelationService.addMemberCommunity(communityId);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("新增区域选择记录失败！", e);
            return ResponseResult.fail("新增区域选择记录失败");
        }
    }
}
